package gui.forms;

import domein.DomeinController;
import gui.NavigatiePanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VoegToeLand extends javax.swing.JFrame{

	private JTree jTree;
	private JTextField jTextFieldCodeLand;
	private JButton jButtonAddLand;
	private JLabel jLabelCodeLand;
	private JLabel jLabelNaam;
	private JTextField jTextFieldNaam;
	private DomeinController domeinController;
	private NavigatiePanel navigatiePanel;
	
	public VoegToeLand(DomeinController domeinController, JTree jTree,NavigatiePanel navigatiePanel) throws IllegalStateException, SQLException, ClassNotFoundException {
		super();
		this.jTree = jTree;
		this.domeinController = domeinController;
		this.navigatiePanel = navigatiePanel;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setBackground(new java.awt.Color(90, 116, 142));
			getContentPane().setLayout(null);
			this.setMaximumSize(new java.awt.Dimension(960, 600));
			this.setMinimumSize(new java.awt.Dimension(960, 600));
			this.setResizable(false);
			this.setLocation(new java.awt.Point(300, 200));
			
			{
				jTextFieldNaam = new JTextField();
				getContentPane().add(jTextFieldNaam);
				jTextFieldNaam.setBounds(123, 19, 164, 35);
			}
			{
				jTextFieldCodeLand = new JTextField();
				getContentPane().add(jTextFieldCodeLand);
				jTextFieldCodeLand.setBounds(123, 73, 164, 35);
			}
			{
				jLabelNaam = new JLabel();
				getContentPane().add(jLabelNaam);
				jLabelNaam.setText("Naam:");
				jLabelNaam.setBounds(30, 33, 60, 16);
			}
			{
				jLabelCodeLand = new JLabel();
				getContentPane().add(jLabelCodeLand);
				jLabelCodeLand.setText("Code land:");
				jLabelCodeLand.setBounds(30, 82, 80, 16);
			}
			{
				jButtonAddLand = new JButton();
				getContentPane().add(jButtonAddLand);
				jButtonAddLand.setText("Voeg land toe");
				jButtonAddLand.setBounds(30, 131, 120, 39);
				jButtonAddLand.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent event) {
						voegToeLand();
					}
				
				});
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void voegToeLand() 
	{
		
		try {	
			
			DefaultMutableTreeNode geselecteerdeNode =(DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();;
	
			if (geselecteerdeNode == null)
			JOptionPane.showMessageDialog(this, "Selecteer een continent, land", "Error",JOptionPane.ERROR_MESSAGE);
	
			String naamLand = jTextFieldNaam.getText();
			String codeLand = jTextFieldCodeLand.getText();		
				
			domeinController.landToevoegen(geselecteerdeNode, naamLand, codeLand);
			jTree.updateUI();
			navigatiePanel.refreshJTreeColors();
			this.dispose();
		
		} 
		
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(null,"Vul de velden correct in");
		}
	}

}
